//
//  ByNetBaseResponseData.m
//  jmsfhz_ios
//
//  Created by pro on 16/8/10.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByNetBaseResponseData.h"

/** 原始数据返回的type为E */
#if DEBUG
/** 返回数据的类型不正常 */
static NSString *errorWithDataTypeDesc = @"数据类型不正常";
static NSInteger errorWithDataTypeCode = -10001;

#else

static NSString *errorWithDataTypeDesc = @"数据异常";
static NSInteger errorWithDataTypeCode = -10001;

#endif
static NSString *errorWithDataEmptyDesc = @"空数据";
static NSInteger errorWithDataEmptyCode = -100001;

static NSString *errorWithDataLonginDesc = @"重登录";
static NSInteger errorWithDataLonginCode = 1111;

@implementation ByNetBaseResponseData

- (id)initWithDic:(NSDictionary *)dic trueType:(ByNetDataType)trueType {
    if (self = [super init]) {
        self.code = [NSString stringWithFormat:@"%@", dic[@"code"]];
        self.data = dic[@"data"];
        self.notify_id = [NSString stringWithFormat:@"%@", dic[@"notify_id"]];
        
        self.trueType = trueType;
    }
    return self;
}

- (void)dataAnalysis {
    
    NSInteger code = [self.code integerValue];          // 数据码
    id data = self.data;
    if ([data isKindOfClass:[NSNumber class]]) {        // 如果data是number类型的，要转换成字符串类型的
        data = [NSString stringWithFormat:@"%@", data];
    }
    
    /** 数据返回正确，做对应处理 */
    if (![data isKindOfClass:[NSNull class]] && code == 0) {

        if ([data isKindOfClass:[NSString class]] && [data isEqualToString:@""]) {
            self.failourBlock(errorWithDataEmptyDesc, errorWithDataEmptyCode);
            return;
        }
        /** 返回数据的数据类型与期望不符合 */
        if (![self chechDataTypeIsTrue:data type:self.trueType]) {
            self.failourBlock(errorWithDataTypeDesc, errorWithDataTypeCode);
            return;
        };
        
        self.successBlock(data);
    } else if (![data isKindOfClass:[NSNull class]] && code == 1111) {
        self.failourBlock(errorWithDataLonginDesc, errorWithDataLonginCode);
        return;
    }
    /** 后台返回code不为0 表示数据返回不正确 */
    else {
        NSString *errorDesc = nil;
        if ([data isKindOfClass:[NSString class]]) {
            /** 长度过长 */
            if ([(NSString *)data length] > 1000) {
                
                errorDesc = @"服务器开小差了，请稍后再试";
            } else {
                errorDesc = data;
            }
        } else if ([data isKindOfClass:[NSNumber class]]) {
            errorDesc = [NSString stringWithFormat:@"%@", data];
        } else {
            errorDesc = @"未知错误!请重试";
        }
        self.failourBlock(errorDesc, code);
    }


}


/** 返回数据是否是正确的 */
- (BOOL)chechDataTypeIsTrue:(id)data type:(ByNetDataType)type {
    
    switch (type) {
        case ByNetDataTypeString:
        {
            if ([data isKindOfClass:[NSString class]]) {
                return YES;
            } else {
                return NO;
            }
        }
            break;
        case ByNetDataTypeArray:
        {
            if ([data isKindOfClass:[NSArray class]]) {
                return YES;
            } else {
                return NO;
            }
        }
            break;
        case ByNetDataTypeDictionary:
        {
            if ([data isKindOfClass:[NSDictionary class]]) {
                return YES;
            } else {
                return NO;
            }
        }
            break;
        default:
            break;
    }
}
@end
